package com.company.编译原理.DFA;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;

/**
 * @ClassName workTwo
 * @Description TODO
 * @Author 11
 * @Date 2021/3/29 14:29
 * @Version 1.0
 */
public class workTwo {


    public static void Solution(StringBuffer y){
        final int s0=0;
        final int s1=1;
        int currState = s0;
        int count=0;
        StringBuffer res = new StringBuffer();//标识符集

        for(int i=0;i<y.length();i++){
            char currChar =y.charAt(i);
            if(currState==s0){
                boolean isletter= Pattern.matches("[A-Za-z][A-Za-z]*", String.valueOf(currChar));//是否字母
                if(isletter){
                    res.append(currChar);
                    currState=s1;
                    continue;
                }

            }
            else if(currState==s1){
                boolean isID=Pattern.matches("[A-Za-z][A-Za-z]*", String.valueOf(currChar));
                if(isID){
                    res.append(currChar);
                    continue;
                }
                count++;
                res.append(" ");
                currState=s0;





            }else{
                System.out.println("error!");
                return;
            }
        }


        System.out.println(res);
        System.out.println("ok "+count);

    }


    public static void main(String[] args) {

        StringBuffer stringBuffer = new StringBuffer();
        BufferedReader reader = null;
        try {
            reader= new BufferedReader(new FileReader("F:\\workProject\\interview-questions\\javacode\\src\\main\\java\\com\\company\\编译原理\\DFA\\test.txt"));
            String tempStr;
            while ((tempStr = reader.readLine()) != null) {
                stringBuffer.append(tempStr);
            }
            reader.close();

            Solution(stringBuffer);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}
